# Variable
set TOP LilyRiscv
set OUT file

# library set
set search_path " . $search_path /home/zwz/proj/LilyRiscv/sim/library"
echo $search_path
set my_sources {/home/zwz/proj/LilyRiscv/rtl/core /home/zwz/proj/LilyRiscv/rtl/perips /home/zwz/proj/LilyRiscv/rtl/soc /home/zwz/proj/LilyRiscv/rtl/utils}
set target_library smic18_ff.db
set link_library   smic18_ff.db

# read rtl
# Warning: in LilyRiscv_top.v, signals over and succ have CMR RISC!. Need comments out some codes
read_file $my_sources -autoread -format verilog -top $TOP
list_designs
current_design
link

# # DDC
# # check IO in current design
# get_ports
create_clock -period 10 -name sys_clk_i [get_ports clk]
set_clock_uncertainty -setup 0.15 [get_ports clk]
set_clock_transition 0.12 [all_clocks]
set_input_delay -max 3 -clock sys_clk_i [get_ports rstn]
set_output_delay -max 3 -clock sys_clk_i [all_outputs]
check_design

compile -scan -incremental

set test_default_delay 0
set test_default_bidir_delay 0
set test_default_strobe 40

create_port scan_in -dir in
create_port scan_out -dir out
create_port scan_en -dir in
create_port test_mode -dir in

set_dft_signal -type ScanDataIn -port scan_in
set_dft_signal -type ScanDataOut -port scan_out
set_dft_signal -type ScanEnable -port scan_en -active_state 1
set_dft_signal -type TestMode -port test_mode -active_state 1
set_dft_signal -view existing_dft -type ScanClock -port [get_ports clk] -timing {45 55}
set_dft_signal -view existing_dft -type Reset -port [get_ports rstn] -active_state 0

# protocol
create_test_protocol

# drc
dft_drc

# check signal
report_scan_state
report_dft_signal -view exist
report_dft_signal -view spec

# check scan chain
preview_dft

# config scan chain
set_scan_configuration -clock_mixing no_mix
set_scan_configuration -style multiplexed_flip_flop
# set_scan_configuration -clock_mixing mix_edges
set_scan_configuration -add_lockup true -clock_mixing mix_clocks
preview_dft -show scan_clocks

# 插入扫描链
insert_dft

# 再次DRC检查
dft_drc -coverage_estimate

# 文件写出
write -f verilog -h -o $OUT/$TOP.netlist
write_scan_def -o $OUT/$TOP.scandef
write_test_protocol -o $OUT/$TOP.spf
write_sdc $OUT/$TOP.sdc
write -format ddc -hier -o $OUT/$TOP.ddc

exit